<?php

/*商品操作相关类
*/
class Product extends MySql{
	
		/*按字段返回数据
		field   string   字段*/
		public function getAllByField($field,$val){
			$sql = $this->select('*','speed_product',$field,$val);
			$data = $this->query($sql,'one');
			return $data;
		}
		
		//返回所有上架商品
		function getShelves(){
			$sql = $this->selectAll("p_id,name,price,path1",'speed_product');
			$sql.=" where state>0 order by id desc";
			return $this->query($sql,'arr');
		}
		
		//返回单个上架商品信息
		function getShelvesByPid($p_id){
			$sql = $this->select('*','speed_product','p_id',$p_id);
			$sql.=" and state>0";
			return $this->query($sql,'one');
		}
		
		//返回所有商品，包括没上架的
		function getAll(){
			$data = $this->query($this->selectAll('*','speed_product'),'arr');
			return $data;
		}
		
		//返回推荐商品
		function getHot(){
			$sql = $this->select('id,p_id,name,path0','speed_product','state',2);
			$sql.=' order by hot_time desc limit 4';
			return $this->query($sql,'arr');
		}
		
		//返回商品ID
		function getProductId($p_id){
			$data = $this->query($this->select('id','speed_product','p_id',$p_id),'one');
			return $data['id'];
		}
		
		function updateProduct($rewrite,$field,$val){
			$sql = $this->update('speed_product',$rewrite,$field,$val);
			$state = $this->query($sql,'none');
			return $state;
		}
		
		function addProduct($val){
			return $this->query($this->insert('speed_product',$val),'none');
		}
		
		function delProduct($id){
			return $this->query($this->del('speed_product','id',$id),'none');
		}
		
		/*返回分类
		id   num  分类编号 默认返回全部*/
		function getTypes($id=0){
			$type = $this->query($this->select('id,name','speed_type','father_id',$id),'arr');
			for($n = 0;$n < count($type);$n++){
				$type1 = $this->query($this->select('id,name','speed_type','father_id',$type[$n]['id']),'arr');
				$son = array();
				for($m = 0;$m < count($type1);$m++){
					$type2 = $this->query($this->select('id,name','speed_type','father_id',$type1[$m]['id']),'arr');
					$son[] = array("id" => $type1[$m]['id'],"name" => $type1[$m]['name'],"son" => $type2);
				}
				$d[$n] = array("id" => $type[$n]['id'],"name" => $type[$n]['name'],"son" => $son);
			}
			//$d = $this->query($this->selectAll('id,father_id,name','speed_type'),'arr');
			return $d;
		}
		
		function addType($val){
			$sql = $this->insert('speed_type',$val);
			$state = $this->query($sql,'none');
			return $state;
		}
		
		function getTypeLink($id){
			$type = $this->query($this->select("*",'speed_type','id',$id),'one');
			$data[] = array("id" => $type['id'],"name" => $type['name']);
			$type = $this->query($this->select("*",'speed_type','id',$type['father_id']),'one');
			$data[] = array("id" => $type['id'],"name" => $type['name']);
			$type = $this->query($this->select("*",'speed_type','id',$type['father_id']),'one');
			$data[] = array("id" => $type['id'],"name" => $type['name']);
			return $data;
		}
		
		function showTypeLink($t_id){
			$data = $this->getTypeLink($t_id);
			$link = '主页->'.$data[2]['name'].'->'.$data[1]['name'];
			return $link;
		}
		
		/*返回同一类不同颜色商品
		p_id   num  商品编号*/
		function getOneType($p_id){
			$type = $this->query($this->select('type','speed_product','p_id',$p_id),'one');
			$sql = $this->select('p_id,color','speed_product','type',$type['type']);
			$sql.=' and state>0';
			$data = $this->query($sql,'arr');
			return $data;
		}
		
		function delType($id){
			return $this->query($this->del('speed_type','id',$id),'none');
		}
		
		function getImage($p_id,$ac='all'){
			$data = array();
			$image = $this->query($this->select('*','speed_product','p_id',$p_id),'one');
			if($ac == 'all'){
				if($image['path0'] != '')$data['path0'] = $image['path0'];else $data['path0'] = '';
				if($image['path1'] != '')$data['path1'] = $image['path1'];else $data['path1'] = '';
				if($image['path2'] != '')$data['path2'] = $image['path2'];else $data['path2'] = '';
				if($image['path3'] != '')$data['path3'] = $image['path3'];else $data['path3'] = '';
				if($image['path4'] != '')$data['path4'] = $image['path4'];else $data['path4'] = '';
				if($image['path5'] != '')$data['path5'] = $image['path5'];else $data['path5'] = '';
			}else if($ac == 'show'){
				if($image['path0'] != '')$data[] = $image['path0'];
				if($image['path1'] != '')$data[] = $image['path1'];
				if($image['path2'] != '')$data[] = $image['path2'];
				if($image['path3'] != '')$data[] = $image['path3'];
				if($image['path4'] != '')$data[] = $image['path4'];
				if($image['path5'] != '')$data[] = $image['path5'];
			}
			return $data;
		}
		
		function getImagePath($id){
			$path = $this->query($this->select('id,path','speed_image','id',$id),'one');
			return $path;
		}
		
		function getImageIdByPath($path){
			$id = $this->query($this->select('id','speed_image','path',$path),'one');
			return $id['id'];
		}
		
		//删除某商品的所有展示图
		function delAllImage($p_id){
			$id = $this->getProductId($p_id);
			$status = $this->query($this->del('speed_product_image','product',$id),'none');
			return $status;
		}
		
		function delImageById($id){
			$status = $this->query($this->del('speed_product_image','id',$id),'none');
			return $status;
		}
		
		function addSina($val){
			return $this->query($this->insert('sina',$val),'none');
		}
		
		function checkSinaExists($iamgeid){
			$sta = $this->query($this->select('*','sina','imageid',$imageid),'num');
			if($sta == 0)return false;
			return true;
		}

	
}

?>